using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Net;
using System.Text;
//+
namespace General.Net
{
    public static class HttpAbstractor
    {
        //- @GetWebText -//
        public static String GetWebText(Uri uri)
        {
            return GetWebText(uri, 0);
        }

        //- @GetWebText -//
        public static String GetWebText(Uri uri, Int32 timeout)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
            if (timeout > 0)
            {
                request.Timeout = timeout * 1000;
            }
            //+
            HttpWebResponse httpWebResponse = (HttpWebResponse)request.GetResponse();
            StringBuilder rawResponse = new StringBuilder();
            using (Stream streamResponse = httpWebResponse.GetResponseStream())
            {

                StreamReader streamRead = new StreamReader(streamResponse);
                //+
                Char[] readBuffer = new Char[256];
                int count = streamRead.Read(readBuffer, 0, 256);
                while (count > 0)
                {
                    String resultData = new String(readBuffer, 0, count);
                    rawResponse.Append(resultData);
                    count = streamRead.Read(readBuffer, 0, 256);
                }
                httpWebResponse.Close();
            }
            //+
            return rawResponse.ToString();
        }

        //- @PostHttpRequest -//
        public static string PostHttpRequest(Uri uri, String text)
        {
            return PostHttpRequest(uri, text, String.Empty, null, 0);
        }

        //- @PostHttpRequest -//
        public static string PostHttpRequest(Uri uri, String text, Int32 timeout)
        {
            return PostHttpRequest(uri, text, String.Empty, null, timeout);
        }

        //- @PostHttpRequest -//
        public static string PostHttpRequest(Uri uri, String text, String contentType, Collection<String> headers, Int32 timeout)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
            request.Method = "POST";
            if (timeout > 0)
            {
                request.Timeout = timeout * 1000;
            }
            if (!String.IsNullOrEmpty(contentType))
            {
                request.ContentType = contentType;
            }
            if (headers != null)
            {
                foreach (String header in headers)
                {
                    request.Headers.Add(header);
                }
            }
            //+
            Byte[] buffer = ASCIIEncoding.UTF8.GetBytes(text);
            request.ContentLength = buffer.Length;
            using (Stream s = request.GetRequestStream())
            {
                s.Write(buffer, 0, buffer.Length);
                s.Close();
            }
            //+
            HttpWebResponse httpWebResponse = (HttpWebResponse)request.GetResponse();
            StringBuilder rawResponse = new StringBuilder();
            using (Stream streamResponse = httpWebResponse.GetResponseStream())
            {

                StreamReader streamRead = new StreamReader(streamResponse);
                Char[] readBuffer = new Char[256];
                Int32 count = streamRead.Read(readBuffer, 0, 256);
                while (count > 0)
                {
                    String resultData = new String(readBuffer, 0, count);
                    rawResponse.Append(resultData);
                    count = streamRead.Read(readBuffer, 0, 256);
                }
                httpWebResponse.Close();
            }
            //+
            return rawResponse.ToString();
        }
    }
}